package com.thingsgrid.workorder.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.thingsgrid.common.cache.CacheNames;
import com.thingsgrid.desk.vo.NoticeVO;
import com.thingsgrid.workorder.entity.ProcessWorkOrder;
import com.thingsgrid.workorder.entity.ProcessWorkOrders;
import com.thingsgrid.workorder.service.IWorkOrderService;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import com.thingsgrid.system.user.cache.UserCache;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 控制器
 *
 * @author MQ
 */
@ApiIgnore
@RestController
@RequestMapping("/process/workorder")
@AllArgsConstructor
public class WorkOrderController extends BladeController implements CacheNames {

	private IWorkOrderService iWorkOrderService;

	/**
	 * 详情
	 *
	 * @param businessId 主键
	 */
	@GetMapping("v1/workorder/content")
	public R<ProcessWorkOrder> detail(@RequestParam("odrId") Long businessId) {
		ProcessWorkOrder detail = iWorkOrderService.getOneWorkOrder(businessId);
		detail.getFlow().setAssigneeName(UserCache.getUser(detail.getCreateUser()).getName());
		return R.data(detail);
	}

	/**
	 * 提交工单信息
	 *
	 * @param processWorkOrder 工单信息
	 */
	@PostMapping("v1/workorder/create")
	public R startProcess(@RequestBody ProcessWorkOrder processWorkOrder) {
		return R.status(iWorkOrderService.startProcess(processWorkOrder));
	}
	/**
	 * 提交工单信息
	 *
	 * @param processWorkOrder 工单信息
	 */
	@GetMapping("v1/workorder/list")
	public R<IPage<ProcessWorkOrder>> workorderLists(@ApiIgnore ProcessWorkOrder processWorkOrder, Query query) {
		IPage<ProcessWorkOrder> processWorkOrders = iWorkOrderService.getListProcessWorkOrders(query,processWorkOrder);
		return R.data(processWorkOrders);
	}
	/**
	 * 分配工单
	 *
	 * @param processWorkOrders 工单信息集合
	 */
	@PostMapping("v1/workorder/assign")
	public R<IPage<ProcessWorkOrder>> wrassign(@ApiIgnore @RequestBody ProcessWorkOrders processWorkOrders) {
		return R.status(iWorkOrderService.disProcess(processWorkOrders.getPwOrders(),processWorkOrders.getTargetId()));
	}
	/**
	 * 处理工单
	 *
	 * @param processWorkOrders 工单信息集合
	 */
	@PostMapping("v1/workorder/hand")
	public R<IPage<ProcessWorkOrder>> handwork(@ApiIgnore @RequestBody List<ProcessWorkOrder> processWorkOrders) {
		return R.status(iWorkOrderService.handProcess(processWorkOrders));
	}
	/**
	 * 关闭工单
	 *
	 * @param processWorkOrders 工单信息集合
	 */
	@PostMapping("v1/workorder/status")
	public R<IPage<ProcessWorkOrder>> wrclose(@ApiIgnore @RequestBody List<ProcessWorkOrder> processWorkOrders) {
		return R.status(iWorkOrderService.closeProcess(processWorkOrders));
	}
}
